using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class PhongDAO:DataProvider
    {
        public DataTable TimPhong(PhongDTO p)
        {
            string sql = "select * from Phong where 1=1 ";
            if (p.Ma != 0)
                sql += " and Ma=" + p.Ma.ToString();
            if (p.Ten != "")
                sql += " and Ten='" + p.Ten.ToString()+"'";
            if (p.MaLoai != 0)
                sql += " and MaLoai=" + p.MaLoai.ToString();
            sql += " and Xoa=" + p.Xoa.ToString();
            DataTable dt = DocBang(sql);
            return dt;
        }
        public DataTable LayMaxMa()
        {
            string sql = "select Max(Ma) from Phong";
            return DocBang(sql);
        }
        public DataTable TraCuuPhong(string sql)
        {
            DataTable dt = DocBang(sql);
            return dt;
        }
        public void Them(PhongDTO  pdto)
        {
            OleDbConnection cn = new OleDbConnection();
            cn = this.KetNoi();
            string str = "Insert into Phong(Ten, MaLoai,GhiChu) values (?, ?, ?)";

            OleDbCommand cmd = new OleDbCommand(str,cn);

            cmd.Parameters.Add("@Ten", OleDbType.WChar);
            cmd.Parameters.Add("@MaLoai",OleDbType.Integer);
            cmd.Parameters.Add("@GhiChu", OleDbType.WChar);

            cmd.Parameters["@Ten"].Value = pdto.Ten;
            cmd.Parameters["@MaLoai"].Value = pdto.MaLoai;
            cmd.Parameters["@GhiChu"].Value = pdto.GhiChu;

            cmd.ExecuteNonQuery();

            cn.Close();
        }
        public void CapNhat(PhongDTO pdto)
        {
            OleDbConnection cn = KetNoi();
            string sql = "Update Phong Set Ten=?,TinhTrang = ?,MaLoai=?,GhiChu=?  Where Ma = ?";
            OleDbCommand cmd = new OleDbCommand(sql, cn);
            cmd.Parameters.Add("@Ten", OleDbType.WChar);
            cmd.Parameters.Add("@TinhTrang", OleDbType.Boolean);
            cmd.Parameters.Add("@MaLoai", OleDbType.Integer);
            cmd.Parameters.Add("@GhiChu", OleDbType.WChar);
            cmd.Parameters.Add("@Ma", OleDbType.Integer);

            cmd.Parameters["@Ten"].Value = pdto.Ten;
            cmd.Parameters["@TinhTrang"].Value = pdto.TinhTrang;
            cmd.Parameters["@MaLoai"].Value = pdto.MaLoai;
            cmd.Parameters["@GhiChu"].Value = pdto.GhiChu;
            cmd.Parameters["@Ma"].Value = pdto.Ma;

            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public void Sua(PhongDTO pdto)
        {
            OleDbConnection cn=KetNoi();
            string sql="Update Phong Set TinhTrang = ?  Where Ma = ?";
            OleDbCommand cmd=new OleDbCommand(sql,cn);
            cmd.Parameters.Add("@TinhTrang", OleDbType.Boolean);
            cmd.Parameters.Add("@Ma", OleDbType.Integer);

            cmd.Parameters["@TinhTrang"].Value = pdto.TinhTrang;
            cmd.Parameters["@Ma"].Value = pdto.Ma;

            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public void Xoa(int map)
        {
            OleDbConnection cn = new OleDbConnection();
            cn = KetNoi();
            string sql = "Update Phong Set Xoa = ?  Where Ma = ?";
            OleDbCommand cmd = new OleDbCommand(sql, cn);

            cmd.Parameters.Add("@Xoa", OleDbType.Boolean);
            cmd.Parameters.Add("@Ma", OleDbType.Integer);

            cmd.Parameters["@Xoa"].Value = true;
            cmd.Parameters["@Ma"].Value = map;

            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public void CapNhatTinhTrang(DataTable dt,string sql)
        {
            UpdateBang(sql, dt);
        }
    }
}
